<template>
  <div  style="width: 100%">
      <header class="header" >
        <el-menu   mode="horizontal" @select="handleSelect" class="center-menu" style="background:  #66b1ff;">
          <el-menu-item  :style="{ backgroundColor: '#66b1ff' }" style="color: white !important;font-size: 30px">汽车租赁</el-menu-item>
          <el-menu-item @click="openIndex('/shouye')"  :style="{ backgroundColor: '#66b1ff' }" class="item1" style="color: white !important;margin-left: 100px;">首页</el-menu-item>
          <el-menu-item @click="openIndex('/zuche')" :style="{ backgroundColor: '#66b1ff'} " style="color: white !important;">租车服务</el-menu-item>
          <el-menu-item @click="openIndex('/carList')" :style="{ backgroundColor: '#66b1ff' }" style="color: white !important;">车圈</el-menu-item>
          <el-menu-item @click="openIndex('/orders')" :style="{ backgroundColor: 'rgb(105,176,250)' }" style="color: white !important;">我的订单</el-menu-item>
          <div style="margin-left: 93%" >
            <el-dropdown>
              <!--                <el-avatar icon="el-icon-user-solid" style="margin-top: 10px"></el-avatar>-->
              <div class="avatar-wrapper">
                <img :src="require('@/assets/images/user.png')" style="width: 40px;height: 40px;margin-top: 10px">
                <i class="el-icon-caret-bottom" />
              </div>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="goToCommentsPage()">个人中心</el-dropdown-item>
                <el-dropdown-item divided @click.native="logout">
                  <span>退出登录</span>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-menu>
      </header>

    <div style="padding: 20px">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="车辆ID" prop="vehicleId">
          <el-input
            v-model="queryParams.vehicleId"
            placeholder="请输入车辆ID"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>



      <el-table v-loading="loading" :data="ordersList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="用户ID" align="center" prop="userId" />
        <el-table-column label="车辆ID" align="center" prop="vehicleId" />
        <el-table-column label="订单日期" align="center" prop="orderDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.orderDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="租赁价格" align="center" prop="rentalPrice" />
        <el-table-column label="租赁时长(/天)" align="center" prop="rentalDuration" />
        <el-table-column label="订单状态" align="center" prop="orderStatus" >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.orderStatus === '0'" :type="danger">已预定</el-tag>
            <el-tag v-if="scope.row.orderStatus === '1'" :type="success">预定成功</el-tag>
            <el-tag v-if="scope.row.orderStatus === '2'" :type="danger">取消预定</el-tag>
            <el-tag v-if="scope.row.orderStatus === '3'" :type="danger">已完成</el-tag>

          </template>
        </el-table-column>
        <el-table-column label="支付状态" align="center" prop="paymentStatus" >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.paymentStatus === '0'" :type="danger">未支付</el-tag>
            <el-tag v-if="scope.row.paymentStatus === '1' " :type="success">已支付</el-tag>
            <el-tag v-if="scope.row.paymentStatus === '2' " :type="danger">已退款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="取车日期" align="center" prop="pickupDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.pickupDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="还车日期" align="center" prop="returnDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.returnDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="订单总金额" align="center" prop="totalAmount" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:orders:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:orders:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改订单信息对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="用户ID" prop="userId">
            <el-input v-model="form.userId" placeholder="请输入用户ID" />
          </el-form-item>
          <el-form-item label="车辆ID" prop="vehicleId">
            <el-input v-model="form.vehicleId" placeholder="请输入车辆ID" />
          </el-form-item>
          <el-form-item label="订单日期" prop="orderDate">
            <el-date-picker clearable
                            v-model="form.orderDate"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择订单日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="租赁价格" prop="rentalPrice">
            <el-input v-model="form.rentalPrice" placeholder="请输入租赁价格" />
          </el-form-item>
          <el-form-item label="租赁时长" prop="rentalDuration">
            <el-input v-model="form.rentalDuration" placeholder="请输入租赁时长" />
          </el-form-item>
          <el-form-item label="取车日期" prop="pickupDate">
            <el-date-picker clearable
                            v-model="form.pickupDate"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择取车日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="还车日期" prop="returnDate">
            <el-date-picker clearable
                            v-model="form.returnDate"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择还车日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="订单总金额" prop="totalAmount">
            <el-input v-model="form.totalAmount" placeholder="请输入订单总金额" />
          </el-form-item>
          <el-form-item label="押金金额" prop="deposit">
            <el-input v-model="form.deposit" placeholder="请输入押金金额" />
          </el-form-item>
          <el-form-item label="折扣金额" prop="discount">
            <el-input v-model="form.discount" placeholder="请输入折扣金额" />
          </el-form-item>
          <el-form-item label="订单创建时间" prop="createdAt">
            <el-date-picker clearable
                            v-model="form.createdAt"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择订单创建时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="订单更新时间" prop="updatedAt">
            <el-date-picker clearable
                            v-model="form.updatedAt"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择订单更新时间">
            </el-date-picker>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>

      <el-dialog :title="yhtitle" :visible.sync="yhopen" width="800px" append-to-body>
        <UserProfile />
      </el-dialog>
    </div>

  </div>
</template>

<script>
import { listOrders, getOrders, delOrders, addOrders, updateOrders } from "@/api/system/orders";
import {getToken} from "@/utils/auth";
import  UserProfile  from '@/views/system/user/profile/yhindex'

export default {
  name: "Orders",
  components: {
    UserProfile
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单信息表格数据
      ordersList: [],
      yhopen: false,
      yhtitle: '用户信息',
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        vehicleId: null,
        orderDate: null,
        rentalPrice: null,
        rentalDuration: null,
        orderStatus: null,
        paymentStatus: null,
        paymentMethod: null,
        pickupDate: null,
        returnDate: null,
        totalAmount: null,
        deposit: null,
        discount: null,
        createdAt: null,
        updatedAt: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
        vehicleId: [
          { required: true, message: "车辆ID不能为空", trigger: "blur" }
        ],
        orderDate: [
          { required: true, message: "订单日期不能为空", trigger: "blur" }
        ],
        rentalPrice: [
          { required: true, message: "租赁价格不能为空", trigger: "blur" }
        ],
        rentalDuration: [
          { required: true, message: "租赁时长不能为空", trigger: "blur" }
        ],
        orderStatus: [
          { required: true, message: "订单状态不能为空", trigger: "change" }
        ],
        paymentStatus: [
          { required: true, message: "支付状态不能为空", trigger: "change" }
        ],
        totalAmount: [
          { required: true, message: "订单总金额不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    if (!getToken()) {
      location.href = '/index'
    }
    this.getList();
  },
  methods: {
    /** 查询订单信息列表 */
    getList() {
      this.loading = true;
      listOrders(this.queryParams).then(response => {
        this.ordersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        userId: null,
        vehicleId: null,
        orderDate: null,
        rentalPrice: null,
        rentalDuration: null,
        orderStatus: null,
        paymentStatus: null,
        paymentMethod: null,
        pickupDate: null,
        returnDate: null,
        totalAmount: null,
        deposit: null,
        discount: null,
        createdAt: null,
        updatedAt: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getOrders(orderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderId != null) {
            updateOrders(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrders(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$modal.confirm('是否确认删除订单信息编号为"' + orderIds + '"的数据项？').then(function() {
        return delOrders(orderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/orders/export', {
        ...this.queryParams
      }, `orders_${new Date().getTime()}.xlsx`)
    },logout() {
      this.$confirm('确定注销并退出系统吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.dispatch('LogOut').then(() => {
          location.href = '/index'
        })
      }).catch(() => {})
    },
    toggleFilter(type) {
      this.selectedType = this.selectedType === type ? 'all' : type
    },
    handlePageChange(page) {
      this.currentPage = page;
    },
    openIndex(index) {
      // 处理预订逻辑
      location.href=index
    },goToCommentsPage() {
      // 处理预订逻辑
      this.yhopen=true
    },
  }
};
</script>
